package com.Vronsky.thread.test01;

import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

@Slf4j(topic = "c.Test21")
public class Test21 {
    public static void main(String[] args) {
        MsgQueue queue=new MsgQueue(1000);
        MsgQueue queue2=new MsgQueue(1000);
//        for (int i = 0; i < 100; i++) {
//            int id=i;
//            new Thread(()->{
//                queue.put(new Msg(id,"值"+id));
//            },"生产者"+i).start();
//        }


        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                int id=i;
                queue.put(new Msg(id,"值"+id));
            }

        },"生产者").start();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                HashMap<String, Object> msg = new HashMap<>();
                msg.put("code",200);
                msg.put("data","队列2的消息"+i);
                msg.put("msg","OK");
                int id=i;
                queue2.put(new Msg(id,msg));
            }

        },"生产者2").start();

        new Thread(()->{
            while (true){
                queue.take();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"消费者1").start();

        new Thread(()->{
            while (true){
                queue2.take();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"消费者2").start();
    }
}

//消息队列类，  java线程间通信
@Slf4j(topic = "c.MsgQueue")
class MsgQueue{
    //消息队列容器
    private Queue<Msg> msgVector=new LinkedList<Msg>();
    //队列容量
    private int capcity;
    //获取消息
    public Msg take(){
        synchronized (msgVector){
            //检查队列是否为空
            while (msgVector.isEmpty()){
                try {
                    log.debug("队列为空，消费者线程等待");
                    msgVector.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //返回头部元素并删除，如果队列为空返回null
            Msg msg = msgVector.poll();
            log.debug("已经消费了消息  {}",msg);
            msgVector.notifyAll();
            return msg;
        }
    }

    //存入消息

    public void put(Msg msg){
        synchronized (msgVector){
            //检查队列是否满了
            while (msgVector.size()==capcity){
                try {
                    log.debug("队列已满，生产者线程等待");
                    msgVector.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //将消息加入队列尾部
            msgVector.add(msg);
            log.debug("已经生产了消息  {}",msg);
            msgVector.notifyAll();
        }

    }
    public MsgQueue(int capcity) {
        this.capcity = capcity;
    }
}


final class Msg{
    private Integer id;
    private Object msg;

    @Override
    public String toString() {
        return "Msg{" +
                "id=" + id +
                ", msg=" + msg +
                '}';
    }

    public Msg(Integer id, Object msg) {

        this.id = id;
        this.msg = msg;
    }

    public Object getMsg() {
        return msg;
    }

    public Integer getId() {
        return id;
    }
}